﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        var renderTable;
        var userType = 0;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            treetable: 'treetable-lay/treetable',
            index: 'lib/index'
        }).use(['table', 'form', 'treetable', 'index'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , element = layui.element
                , treetable = layui.treetable;
            element.render();

            // 渲染表格
            renderTable = function () {
                layer.load(2);
                treetable.render({
                    elem: '#table-datalist'
                    , treeColIndex: 1
                    , treeSpid: 0
                    , defaultToolbar: false
                    , treeIdName: 'id'
                    , treePidName: 'pid'
                    , url: '/Console/RebateLevel/GetListJson?schemeId=@ViewBag.schemeId'
                    , toolbar: '#table-datalist-toolbar'
                    , limit: 20
                    , title: '数据表'
                    , cols: [[
                        { type: 'checkbox', fixed: 'left' }
                        , { field: 'names', title: '名称', width: 200, style: 'text-align:left' }
                        , { field: 'cardinal', title: '返佣基数', style: 'text-align:left' }
                        , { field: 'remark', title: '备注', style: 'text-align:left' }
                        , { field: 'creator', title: '创建人' }
                        , { field: 'createDate', title: '添加时间', width: 180 }
                        , { field: 'sort', title: '排序' }
                        , {
                            field: 'state', title: '状态', width: 110, templet: function (res) {
                                if (res.state == 1)
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '" checked="checked">'
                                else
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '">'
                            }
                        }
                        , { field: 'operate', fixed: 'right', title: '操作', style: 'text-align:left', toolbar: '#table-datalist-bar', width: 220 }
                    ]]
                    , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                    , done: function (res) {
                        if (res.count == 0) {
                            $(".layui-table-main").html('<div class="layui-none">' + top.lang.noData + '</div>');
                        }
                        layer.closeAll('loading');

                        /**多语言初始化 */
                        top.SetListPageLanguage(layui);
                    }
                });
            };
            //加载表格
            renderTable();

            //头工具栏事件
            table.on('toolbar(table-datalist)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'addNew':
                        top.OpenPageByKey(layui, "addNew", 2, '600px', '600px', '/Console/RebateLevel/CreateLevel?schemeId=@ViewBag.schemeId');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    var postData = {
                        id: data.id
                    };
                    layui.layer.confirm(top.lang.confirmDelete, {
                        title: top.lang.confirmTitle,
                        btn: [top.lang.confirmBtn, top.lang.closeBtn]
                    }, function (index, layero) {
                        $.ajax({
                            type: "POST",
                            url: "/Console/RebateLevel/Delete",
                            data: postData, // 你的formid
                            async: true,
                            error: function (request) {
                                alert("Connection error");
                            },
                            beforeSend: function (xhr) {
                                layui.layer.load();
                            },
                            success: function (data) {
                                var item = eval(data);
                                if (item.code === 0) {
                                    top.ShowMessageByKey(layui, "deleteSuccess", 6, 1000, function () {
                                        renderTable();
                                    });
                                } else {
                                    top.AlertByKey(layui, item.msg, 0);
                                }
                                layer.closeAll('loading');
                            }
                        });
                    });

                } else if (obj.event === 'edit') {
                    if (data.pid === 0) {
                        top.OpenPageByKey(layui, "editMenus", 2, '600px', '600px', '/Console/RebateLevel/EditLevel?id=' + data.id);
                    } else {
                        top.OpenPageByKey(layui, "editMenus", 2, '600px', '600px', '/Console/RebateLevel/Edit?id=' + data.id);
                    }
                }
                else if (obj.event === 'rebateSettings') {
                    top.OpenPageByKey(layui, "addNew", 2, '600px', '600px', '/Console/RebateLevel/Create?pid=' + data.id + '&schemeId=@ViewBag.schemeId');
                }
            });

            //监听锁定操作
            form.on('switch(table-state)', function (obj) {
                var state = 1;
                if (obj.elem.checked) {
                    state = 1;
                } else {
                    state = 0;
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/RebateLevel/UpdateState",
                    data: {
                        id: this.value
                        , intState: state
                    }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, "submitFail", 0);
                        }
                        layer.closeAll('loading');
                    }
                });
            });

        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                    <script type="text/html" id="table-datalist-toolbar">
                        <div class="layui-btn-container" id="layui-btn-container">
                            <button class="layui-btn layui-btn-sm" lay-event="addNew">新增</button>
                        </div>
                    </script>
                    <script type="text/html" id="table-datalist-bar">
                        <button class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
                        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
                        {{#if(d.pid==0){}}
                        <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="rebateSettings">返佣设置</button>
                        {{#}}}
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
</div>